/**
 * <p>Title: MovableObstacle.java</p>
 * <p>Description: CS 343 Final Project. This class contains the movable obstacle for the game</p>
 * @author Tim Olmstead, Brady Fetting, Erik Brown, Thales Kirk
 * @email timothy.olmstead@my.uwrf.edu, brady.fetting@my.uwrf.edu, 
 * 			erik.brown@my.uwrf.edu, thales.kirk@my.uwrf.edu
 * @date November 15, 2013
 */
package Obstacles;

import java.awt.Image;
import java.awt.Polygon;
import javax.swing.ImageIcon;
import GameState.Game;
import Settings.Config;

public class MoveableObstacle extends Obstacle {

	private Image sprite;
	private int obstacleType;
	private final int BEEHIVE = 0;
	private final int BABYSTROLLER = 1;
	private final int CRATE = 2;
	private final int LAWNCHAIR = 3;
	private final int HOMELESSMAN = 4;
	private final int LOGS = 5;
	private final int PARKBENCH = 6;
	private final int PARKSIGN = 7;
	private final int ROCK = 8;

	public MoveableObstacle() {
		// default set to 10 X 10 but set actaul size after
		super(Config.FRAME_WIDTH + 20, Game.getGroundLevel() - 50, 1000, 1000);
		sprite = new ImageIcon(getRandomFileName()).getImage();
		super.setHeight(sprite.getHeight(null));
		super.setWidth(sprite.getWidth(null));
		setYloc(getAdjustedHeight());
	}

	@Override
	/**
	 * @param none
	 * @description gets the image of the sprite obstacle
	 */
	public synchronized Image getImage() {
		return sprite;
	}

	/**
	 * @param none
	 * @description gets a random obstacle to spawn
	 */
	private String getRandomFileName() {
		String[] pics = new String[9];
		pics[0] = "src/images/beeHive.png";
		pics[1] = "src/images/babyStroller.png";
		pics[2] = "src/images/crate.png";
		pics[3] = "src/images/lawnChair.png";
		pics[4] = "src/images/homelessMan.png";
		pics[5] = "src/images/logs.png";
		pics[6] = "src/images/parkBench.png";
		pics[7] = "src/images/parkSign.png";
		pics[8] = "src/images/rock.png";

		obstacleType = (int) (Math.random() * pics.length);

		return pics[obstacleType];
	}

	@Override
	/**
	 * @param none
	 * @description moves the spawned obstacle
	 */
	public synchronized void move() {
		super.setXloc(super.getXloc() - Game.getGameSpeed());

	}

	/**
	 * @param none
	 * @description gets the adjusted height of the obstacle
	 */
	private int getAdjustedHeight() {
		int y;
		switch (obstacleType) {
		case BEEHIVE:
			y = Game.getGroundLevel() + 80;
			break;
		case BABYSTROLLER:
			y = Game.getGroundLevel() + 60;
			break;
		case CRATE:
			y = Game.getGroundLevel() + 50;
			break;
		case LAWNCHAIR:
			y = Game.getGroundLevel() + 80;
			break;
		case HOMELESSMAN:
			y = Game.getGroundLevel() + 55;
			break;
		case LOGS:
			y = Game.getGroundLevel() + 90;
			break;
		case PARKBENCH:
			y = Game.getGroundLevel() + 60;
			break;
		case PARKSIGN:
			y = Game.getGroundLevel() + 45;
			break;
		case ROCK:
			y = Game.getGroundLevel() + 65;
			break;
		default:
			y = 0;
		}
		return y;
	}

	@Override
	/**
	 * @param none
	 * @description sets the polygon border of the obstacle for collision detection
	 */
	public Polygon getPolygon() {
		switch (obstacleType) {
		case BEEHIVE:
			return beehivePolygon();
		case BABYSTROLLER:
			return babystrollerPolygon();
		case CRATE:
			return cratePolygon();
		case LAWNCHAIR:
			return lawnchairPolygon();
		case HOMELESSMAN:
			return homelessManPolygon();
		case LOGS:
			return logsPolygon();
		case PARKBENCH:
			return parkBenchPolygon();
		case PARKSIGN:
			return parkSignPolygon();
		case ROCK:
			return rockPolygon();
		}
		return null;
	}

	/**
	 * @param none
	 * @description sets the polygon border of the beehive
	 */
	private Polygon beehivePolygon() {
		int[] xs = new int[5];
		int[] ys = new int[5];
		xs[0] = getXloc() + 0;
		ys[0] = getYloc() + 9;
		xs[1] = getXloc() + 39;
		ys[1] = getYloc() + 18;
		xs[2] = getXloc() + 15;
		ys[2] = getYloc() + 37;
		xs[3] = getXloc() + 5;
		ys[3] = getYloc() + 35;
		xs[4] = getXloc() + 3;
		ys[4] = getYloc() + 20;
		return new Polygon(xs, ys, xs.length);
	}

	/**
	 * @param none
	 * @description sets the polygon border of the babystroller
	 */
	private Polygon babystrollerPolygon() {
		int[] xs = new int[5];
		int[] ys = new int[5];
		xs[0] = getXloc() + 11;
		ys[0] = getYloc() + 9;
		xs[1] = getXloc() + 41;
		ys[1] = getYloc() + 4;
		xs[2] = getXloc() + 55;
		ys[2] = getYloc() + 21;
		xs[3] = getXloc() + 48;
		ys[3] = getYloc() + 56;
		xs[4] = getXloc() + 8;
		ys[4] = getYloc() + 56;
		return new Polygon(xs, ys, xs.length);
	}

	/**
	 * @param none
	 * @description sets the polygon border of the crate
	 */
	private Polygon cratePolygon() {
		int[] xs = new int[5];
		int[] ys = new int[5];
		xs[0] = getXloc() + 8;
		ys[0] = getYloc() + 1;
		xs[1] = getXloc() + 64;
		ys[1] = getYloc() + 7;
		xs[2] = getXloc() + 63;
		ys[2] = getYloc() + 60;
		xs[3] = getXloc() + 10;
		ys[3] = getYloc() + 70;
		xs[4] = getXloc() + 2;
		ys[4] = getYloc() + 53;
		return new Polygon(xs, ys, xs.length);
	}

	/**
	 * @param none
	 * @description sets the polygon border of the lawnchair
	 */
	private Polygon lawnchairPolygon() {
		int[] xs = new int[6];
		int[] ys = new int[6];
		xs[0] = getXloc() + 27;
		ys[0] = getYloc() + 0;
		xs[1] = getXloc() + 50;
		ys[1] = getYloc() + 2;
		xs[2] = getXloc() + 44;
		ys[2] = getYloc() + 37;
		xs[3] = getXloc() + 22;
		ys[3] = getYloc() + 50;
		xs[4] = getXloc() + 0;
		ys[4] = getYloc() + 43;
		xs[5] = getXloc() + 1;
		ys[5] = getYloc() + 22;
		return new Polygon(xs, ys, xs.length);
	}

	/**
	 * @param none
	 * @description sets the polygon border of the homeless man
	 */
	private Polygon homelessManPolygon() {
		int[] xs = new int[6];
		int[] ys = new int[6];
		xs[0] = getXloc() + 0;
		ys[0] = getYloc() + 0;
		xs[1] = getXloc() + 10;
		ys[1] = getYloc() + 0;
		xs[2] = getXloc() + 50;
		ys[2] = getYloc() + 14;
		xs[3] = getXloc() + 75;
		ys[3] = getYloc() + 40;
		xs[4] = getXloc() + 63;
		ys[4] = getYloc() + 70;
		xs[5] = getXloc() + 10;
		ys[5] = getYloc() + 70;
		return new Polygon(xs, ys, xs.length);
	}

	/**
	 * @param none
	 * @description sets the polygon border of the logs
	 */
	private Polygon logsPolygon() {
		int[] xs = new int[4];
		int[] ys = new int[4];
		xs[0] = getXloc() + 5;
		ys[0] = getYloc() + 3;
		xs[1] = getXloc() + 41;
		ys[1] = getYloc() + 1;
		xs[2] = getXloc() + 49;
		ys[2] = getYloc() + 23;
		xs[3] = getXloc() + 0;
		ys[3] = getYloc() + 26;
		return new Polygon(xs, ys, xs.length);
	}

	/**
	 * @param none
	 * @description sets the polygon border of the park bench
	 */
	private Polygon parkBenchPolygon() {
		int[] xs = new int[5];
		int[] ys = new int[5];
		xs[0] = getXloc() + 0;
		ys[0] = getYloc() + 0;
		xs[1] = getXloc() + 60;
		ys[1] = getYloc() + 2;
		xs[2] = getXloc() + 81;
		ys[2] = getYloc() + 50;
		xs[3] = getXloc() + 29;
		ys[3] = getYloc() + 75;
		xs[4] = getXloc() + 2;
		ys[4] = getYloc() + 60;
		return new Polygon(xs, ys, xs.length);
	}

	/**
	 * @param none
	 * @description sets the polygon border of the park sign
	 */
	private Polygon parkSignPolygon() {
		int[] xs = new int[5];
		int[] ys = new int[5];
		xs[0] = getXloc() + 17;
		ys[0] = getYloc() + 0;
		xs[1] = getXloc() + 145;
		ys[1] = getYloc() + 11;
		xs[2] = getXloc() + 136;
		ys[2] = getYloc() + 97;
		xs[3] = getXloc() + 0;
		ys[3] = getYloc() + 78;
		xs[4] = getXloc() + 0;
		ys[4] = getYloc() + 5;
		return new Polygon(xs, ys, xs.length);
	}

	/**
	 * @param none
	 * @description sets the polygon border of the rock
	 */
	private Polygon rockPolygon() {
		int[] xs = new int[6];
		int[] ys = new int[6];
		xs[0] = getXloc() + 20;
		ys[0] = getYloc() + 0;
		xs[1] = getXloc() + 60;
		ys[1] = getYloc() + 0;
		xs[2] = getXloc() + 75;
		ys[2] = getYloc() + 34;
		xs[3] = getXloc() + 52;
		ys[3] = getYloc() + 62;
		xs[4] = getXloc() + 20;
		ys[4] = getYloc() + 62;
		xs[5] = getXloc() + 0;
		ys[5] = getYloc() + 32;
		return new Polygon(xs, ys, xs.length);
	}

}
